const bKey = 'book_key';
const cKey = 'category_key';

export default{
  state:{
    // 所有书籍列表
    bookList: JSON.parse(localStorage.getItem(bKey)) || [],
    // 轮播图底部推荐
    swiperBottomList: [],
    // 分类列表展示
    cateGoryList: JSON.parse(localStorage.getItem(cKey)) || []
  },
  getters:{
    // 获取所有书名 便于查找
    getBooksNameAndId( state ){
      return state.bookList.map(item => ({
        book_id: item.book_id,
        book_name: item.book_name
      }));
    }
  },
  mutations:{
    setAllBookList(state,data){
      if(state.bookList.length != 0){
        localStorage.removeItem(bKey);
      }
      state.bookList = data;
      // 需要做持久化存储
      localStorage.setItem(bKey, JSON.stringify(data));
    },
    setSwiperBottomList(state,data){
      state.swiperBottomList = data;
    },
    setCateGoryList(state,data){
      // 需要做持久化存储
      state.cateGoryList = data;
      localStorage.setItem(cKey, JSON.stringify(data));
    }
  },
  actions:{
    setNewBookList({ commit }, data){
      commit('setAllBookList', data);
    }
  },
}
